<!doctype html>
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<html>
<head>
  <meta charset="utf-8">
  <script src="../../node_modules/@webcomponents/webcomponentsjs/webcomponents-bundle.js"></script>
  <script src="wct-browser-config.js"></script>
  <script src="../../node_modules/wct-browser-legacy/browser.js"></script>
  <script type="module" src="../../polymer-legacy.js"></script>
<body>

<x-foo prop1="a" prop2="b"></x-foo>

<script type="module">
import { PropertyAccessors } from '../../lib/mixins/property-accessors.js';
class XFoo extends PropertyAccessors(HTMLElement) {
  static get observedAttributes() {
    return ['prop1', 'prop2'];
  }
  constructor() {
    super();
    this._propertiesChanged = sinon.spy();
  }
  connectedCallback() {
    this._enableProperties();
  }
}
XFoo.createPropertiesForAttributes();
window.XFoo = XFoo;
customElements.define('x-foo', XFoo);
</script>

<script type="module">

suite('property-accessors', function() {

  test('createPropertiesForAttributes creates accessors', function() {
    assert.ok(Object.getOwnPropertyDescriptor(window.XFoo.prototype, 'prop1'));
    assert.ok(Object.getOwnPropertyDescriptor(window.XFoo.prototype, 'prop2'));
  });

  test('attributes reflected to properties via upgrade', function() {
    var el = document.querySelector('x-foo');
    assert.equal(el.prop1, 'a');
    assert.equal(el.prop2, 'b');
  });

  test('setting properties results in _propertiesChanged', function(done) {
    var el = document.createElement('x-foo');
    document.body.appendChild(el);
    el.prop1 = 'a';
    el.prop2 = 'b';
    assert.equal(el._propertiesChanged.callCount, 0, '_propertiesChanged is not async');
    setTimeout(function() {
      assert.isTrue(el._propertiesChanged.calledOnce);
      assert.equal(el._propertiesChanged.getCall(0).args[0].prop1, 'a');
      assert.equal(el._propertiesChanged.getCall(0).args[0].prop2, 'b');
      assert.equal(el._propertiesChanged.getCall(0).args[1].prop1, 'a');
      assert.equal(el._propertiesChanged.getCall(0).args[1].prop2, 'b');
      assert.equal(el._propertiesChanged.getCall(0).args[2].prop1, undefined);
      assert.equal(el._propertiesChanged.getCall(0).args[2].prop2, undefined);
      assert.equal(el.prop1, 'a');
      assert.equal(el.prop2, 'b');
      el.prop1 = 'aa';
      setTimeout(function() {
        assert.isTrue(el._propertiesChanged.calledTwice);
        assert.equal(el._propertiesChanged.getCall(1).args[0].prop1, 'aa');
        assert.equal(el._propertiesChanged.getCall(1).args[0].prop2, 'b');
        assert.equal(el._propertiesChanged.getCall(1).args[1].prop1, 'aa');
        assert.equal(el._propertiesChanged.getCall(1).args[2].prop1, 'a');
        assert.isFalse('prop2' in el._propertiesChanged.getCall(1).args[1]);
        assert.isFalse('prop2' in el._propertiesChanged.getCall(1).args[2]);
        done();
      });
    });
  });

  test('setting attributes results in _propertiesChanged', function(done) {
    var el = document.createElement('x-foo');
    document.body.appendChild(el);
    el.setAttribute('prop1', 'a');
    el.setAttribute('prop2', 'b');
    setTimeout(function() {
      assert.isTrue(el._propertiesChanged.calledOnce);
      assert.equal(el._propertiesChanged.getCall(0).args[0].prop1, 'a');
      assert.equal(el._propertiesChanged.getCall(0).args[0].prop2, 'b');
      assert.equal(el._propertiesChanged.getCall(0).args[1].prop1, 'a');
      assert.equal(el._propertiesChanged.getCall(0).args[1].prop2, 'b');
      assert.equal(el._propertiesChanged.getCall(0).args[2].prop1, undefined);
      assert.equal(el._propertiesChanged.getCall(0).args[2].prop2, undefined);
      assert.equal(el.prop1, 'a');
      assert.equal(el.prop2, 'b');
      el.setAttribute('prop1', 'aa');
      setTimeout(function() {
        assert.isTrue(el._propertiesChanged.calledTwice);
        assert.equal(el._propertiesChanged.getCall(1).args[0].prop1, 'aa');
        assert.equal(el._propertiesChanged.getCall(1).args[0].prop2, 'b');
        assert.equal(el._propertiesChanged.getCall(1).args[1].prop1, 'aa');
        assert.equal(el._propertiesChanged.getCall(1).args[2].prop1, 'a');
        assert.isFalse('prop2' in el._propertiesChanged.getCall(1).args[1]);
        assert.isFalse('prop2' in el._propertiesChanged.getCall(1).args[2]);
        done();
      });

    });
  });

  suite('testing for deserialization of date', function() {
    let date;

    setup(function() {
      date = new Date();
    });

    test('can handle string timestamp', function() {
      const deserializedDate = window.XFoo.prototype._deserializeValue(String(date.getTime()), Date);
      assert.equal(deserializedDate.getTime(), date.getTime());
    });

    test('can handle number timestamp', function() {
      const deserializedDate = window.XFoo.prototype._deserializeValue(date.getTime(), Date);
      assert.equal(deserializedDate.getTime(), date.getTime());
    });

    test('can handle full date', function() {
      const deserializedDate = window.XFoo.prototype._deserializeValue(date.toString(), Date);
      assert.equal(deserializedDate.toString(), date.toString());
    });
  });

});
</script>

</body>
</html>
